package test30;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2023-01-02
 * Time: 22:04
 */
class TreeNode{

    public TreeNode(int data) {
        this.data = data;
    }

    int data;
    TreeNode left;
    TreeNode right;
}
//镜像二叉树
public class Main {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(0);
        TreeNode root1 = new TreeNode(1);
        TreeNode root2 = new TreeNode(1);
        TreeNode root3 = new TreeNode(3);
        TreeNode root4 = new TreeNode(3);

        root.left = root1;
        root.right = root2;
        root1.right = root3;
        root2.left = root4;
        boolean flag = issymmetry(root);
        System.out.println(flag);
    }

    //判断二叉树是否对称
    private static boolean issymmetry(TreeNode root) {
        if(root == null) return false;
        return dfs(root.left,root.right);
    }

    private static boolean dfs(TreeNode left, TreeNode right) {
        if(left == null && right == null) return true;
        if(left == null && right != null) return false;
        if(left != null && right == null) return false;
        if(left.data != right.data) return false;
        return dfs(left.left,right.right) && dfs(left.right, right.left);

    }
}
